<?php

class Flat_modul  extends CI_Model {
	public $select;
	public $dollars;
 
	public function __construct() {
		// Call the Model constructor
		parent::__construct();

		$this -> fproperty();
		$this-> dollars=30;
	}

	/**
	 * Поиск наглавной главная
	 *
	 * @return void
	 * @author
	 */

	public function request($get = false, $start = 0, $finish = 30) {

		$data = false;
		$select_data = array();
		$DB2 = $this -> load -> database('adsent', TRUE, TRUE);
		$req = $get;
		$select = "SELECT    
    r.id,
    r.root,
    r.kitchen,
    r.area,
    r.l_area,
    r.t_area,
    r.adress_name,
    r.region_names,
    r.region_id,
    r.city_name,
    r.city_id,
    r.street_name,
    r.street_id,
    r.home_id,
    r.id_metro,
    r.min_user,
    r.goby,
    r.price,
    
    r.currency,
    r.prepay,
    r.zalog,
    
    r.newtime,
    
    
    r.for_days,
    r.d_price,
    r.d_currency,
    r.d_comment,
    r.floor,
    r.floorn,
    r.mebel,
    r.mebel_k,
    r.phone,
    r.tv,
    r.rfgr,
    r.wm,
    r.balkon,
    r.pet,
    r.kids,
    r.comment_client,
    r.comment_site,
    r.keepday,
    r.points,
    r.for_all,
    r.contact_telefon1,
    r.contact_name1,
    r.contact_telefon2,
    r.contact_name2,
    r.id_phone,
    r.act,
    r.datetime,
    r.moderator,
    r.id_metro,
    r.name_metro
    
   FROM `rflat` `r` 
    
   $req 
   
   GROUP BY r.id
   LIMIT $start,$finish;";
		$q = $DB2 -> query($select);
		$data = $this -> req($q);
		return $data;
	}

	public function fproperty() {
		$lt = false;

		$DB2 = $this -> load -> database('adsent', TRUE, TRUE);
		$query = $DB2 -> query("SELECT * FROM `property`");

		if ($query -> num_rows() > 0) {
			foreach ($query->result() as $row) {
				$lt[$row -> text] = $row -> val;
			}
		}
		$this -> property = $lt;
		return;
	}

	/**
	 *
	 * @param $array - массив с данными для разбора
	 *
	 * */
	public function req($datas) {
		#	 $data =  array();

		$this -> load -> helper('text');
		$this -> load -> helper('see');
		if ($datas -> num_rows() > 0) {

			foreach ($datas -> result() as $row) {

				$data[] = array('id' => $row -> id, 'root' => rent_flat_object_flat($row -> root), 'r_kitchen' => $row -> kitchen, 'area' => $row -> area, 'l_area' => $row -> l_area, 't_area' => $row -> t_area, 'r_adress_name' => $row -> adress_name, 'r_region_names' => $row -> region_names, 'r_region_id' => $row -> region_id, 'r_city_name' => $row -> city_name, 'r_city_id' => $row -> city_id, 'r_street_name' => $row -> street_name, 'r_street_id' => $row -> street_id, 'r_home_id' => $row -> home_id, 'r_id_metro' => $row -> id_metro, 'r_min_user' => $row -> min_user, 'newtime' => $row -> newtime,
				// пешком до метро
				'goby' => rent_flat_metro_distance($row -> goby), 
				'price_dollar' => rent_flat_current($row -> price, $row -> currency, 'dollars',$this->dollars), 'price_rubley' => rent_flat_current($row -> price, $row -> currency, 'rubley',$this->dollars), 'r_currency' => $row -> currency, 'r_prepay' => $row -> prepay,
				 'r_zalog' => $row -> zalog, 'r_for_days' => $row -> for_days,
				  'r_d_price' => $row -> d_price, 'currency' => $row -> currency, 
				  'r_d_comment' => $row -> d_comment, 'floor' => $row -> floor,
				   'floorn' => $row -> floorn, 'mebel' => $row -> mebel,
				    'mebel_k' => $row -> mebel_k, 'phone' => $row -> phone,
				     'tv' => $row -> tv, 'rfgr' => $row -> rfgr, 'wm' => $row -> wm, 
				     'balkon' => $row -> balkon, 'pet' => $row -> pet, 'kids' => $row -> kids,
				      'comment_client' => $row -> comment_client, 'r_comment_site' => $row -> comment_site,
				       'r_keepday' => $row -> keepday, 'for_day' => rent_flat_for_days($row -> for_all), 
				       'contact_telefon1' => $row -> contact_telefon1, 
				       'contact_name1' => $row -> contact_name1, 'contact_telefon2' => $row -> contact_telefon2, 'contact_name2' => $row -> contact_name2, 'r_id_phone' => $row -> id_phone, 'r_act' => $row -> act, 'datetime' => $row -> datetime, 'r_moderator' => $row -> moderator, 'metro' => $row -> name_metro, 'metro_id' => $row -> id_metro, 'adsent' => 'rflat', );
			}
			return $data;
		}
		return 0;

	}

}
